<?php

class Gallery_Block_Breadcrumbs extends Kernel_Block 
{
    
    /**
     * Return template data. 
     *
     * @return  array
     */
    public function getData() 
    {
        $query = '
                SELECT 
                       `id`, 
                       `parent_id`, 
                       `name`, 
                       `url`, 
                       IF(`url` = "' . Application::getRequest()->uri(1) . '", 1, 0) `active`
                  FROM 
                       `photo_categories` 
              ORDER BY 
                       `position` 
            ';
        
        $breadcrumbs = array();
        $this->_recursive(Kernel_Util_Tree::convert(database::fetchAll($query)), $breadcrumbs);
        
        return array('entries' => $breadcrumbs);
    }
    
    private function _recursive(array $entries, array &$breadcrumbs) 
    {
        foreach ($entries as $entry) {
            if ($entry['active']) {
                $breadcrumbs[] = array
                (
                    'id' => $entry['id'], 
                    'name' => $entry['name'], 
                    'url' => $entry['url'], 
                );
                if (!empty($entry['children'])) {
                    $this->_recursive($entry['children'], $breadcrumbs);
                }
            }
        }
    }
    
}